/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.com.jsepc.rep.report;

import cn.com.jsepc.rep.comm.ConstUtil;
import cn.com.jsepc.rep.demension.CheckLeaf;
import cn.com.jsepc.rep.vo.RepDataVO;
import cn.com.jsepc.rep.vo.RepDemensionVO;
import com.jarp.db.DAO;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;
import java.util.Set;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 是否参照上期模版和是否显示上期值
 *
 * @author zhaoyuan
 */
public class FillDataSQ {

    /**
     * 获取当前表格的数据
     * @param start
     * @param limit
     * @param repid
     * @param corpid
     * @param ishow
     * @return 
     */
    public String getGridData(String start, String limit, String repid, String corpid, String ishow) {
        String res = "";
        boolean isshow = Boolean.valueOf(ishow);
        String facttable = "";
        DAO dao = new DAO();
        try {
            //获取事实表表名
            facttable = dao.getDataValue(ConstUtil.DBNAME, "SELECT t.REFFACT from tb_report t WHERE t.REPID=" + repid);
        } catch (ServletException ex) {
            Logger.getLogger(FillDataSQ.class.getName()).log(Level.SEVERE, null, ex);
        }
        //获取上期值时间
        String lastTime = getSQ(repid);
        //获取本期值
        String thistime = getThis(repid);
        //获取时间字段
        String yearcol = getTimeCol(repid);
        //获取维度列表,即上期值所筛选的维度
        //   String demJsonArr = this.getDemlist(facttable, repid, lastTime, yearcol, this.getThis(repid), isshow);
        JSONArray json = new JSONArray();
        String totalCount = null;
        int iStart = Integer.parseInt(start);
        int iPageSize = Integer.parseInt(limit);
        int iPage = iStart / iPageSize + 1;
        RepDataVO repVo = getRepDemension(repid);
        if (repVo != null) {
            String tablename = repVo.getReffact(); //TF_REP_HRCNT
            String faccol = repVo.getRepvaluecol(); //HTCNT
            String facsumcol = "SUMVAL";
            String dwDimCodeCol = null;
            String dwDimCodeVal = null;
            boolean bDwDim = false;
            ArrayList<RepDemensionVO> repDemLst = repVo.getRepDemLst();
            String timecolid = "";    //时间字段的id
            String timecolname = "";  //时间字段的name
            ArrayList<String> demid = new ArrayList<String>();
            ArrayList<String> demnamecol = new ArrayList<String>();
            ArrayList<String> facdemcol = new ArrayList<String>();
            ArrayList<String> demname = new ArrayList<String>();
            ArrayList<String> demtable = new ArrayList<String>();
            ArrayList<String> demtableNamecol = new ArrayList<String>();
            ArrayList<String> demtableidcol = new ArrayList<String>();
            for (int j = 0; j < repDemLst.size(); j++) {
                RepDemensionVO repDemVO = repDemLst.get(j);
                String dimidcol = repDemVO.getIdcol();
                String facdimcol = repDemVO.getFactdimcol();  //ORG_ID
                String dimnamecol = repDemVO.getNamecol();
                String dimtype = repDemVO.getDemtype();//1单位维度 2时间维度 3其他维度
                String dimname=repDemVO.getDemname();
                demtable.add(repDemVO.getDimtable());
                demtableNamecol.add(repDemVO.getNamecol());
                demtableidcol.add(repDemVO.getIdcol());
                if(dimtype.equals("2")){
                    timecolid=facdimcol;
                    timecolname=dimnamecol;
                }
                demid.add(dimidcol);
                demnamecol.add(dimnamecol);
                facdemcol.add(facdimcol);
                demname.add(dimname);
            }
            String sql="select ";
            for(String str:facdemcol){
                sql+=str+",";
            }
            if(isshow){   //包含值
             sql+="repval,sumval,preval,tpreval,tsumval,trepval from "+facttable+" where "+timecolid+"="+lastTime+" and (repval is not null or sumval is not null)";   
            }else{
                sql=sql.substring(0, sql.length()-1);
                sql+=" from "+facttable+" where "+timecolid+"="+lastTime+" and (repval is not null or sumval is not null)";  
            }
            try {
                Vector v=dao.getDataSet(ConstUtil.DBNAME, sql,iPage,iPageSize);
                if(v!=null){
                    for(int i=0;i<v.size();i++){
                        Hashtable hash=(Hashtable) v.get(i);
                        for(int i2=0;i2<demnamecol.size();i2++){
                            hash.put(facdemcol.get(i2)+demnamecol.get(i2), this.getDimNamebyId(demtable.get(i2), demtableNamecol.get(i2), demtableidcol.get(i2), (String)hash.get(facdemcol.get(i2))));
                            hash.put(timecolid, thistime);
                            hash.put(timecolname, this.getTimeColName(repid, thistime));
                        }
                        v.set(i, hash);
                    }
                }
                 json = JSONArray.fromObject(v);
                 int count=dao.getDataSetCount(ConstUtil.DBNAME, sql);
                  res = "{totalCount:" + count + ",data:" + json.toString() + "}";
            } catch (ServletException ex) {
                Logger.getLogger(FillDataSQ.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return res;
    }
    
    /**
     * 获取所有数据，用来更新所有的上期值
     * @param repid
     * @param corpid
     * @param ishow
     * @return 
     */
    public String getGridData( String repid, String roleid,String objInfo) {
        String res = "";
        String facttable = "";
        boolean isSuccessed=true;
        DAO dao = new DAO();
        try {
            //获取事实表表名
            facttable = dao.getDataValue(ConstUtil.DBNAME, "SELECT t.REFFACT from tb_report t WHERE t.REPID=" + repid);
        } catch (ServletException ex) {
            Logger.getLogger(FillDataSQ.class.getName()).log(Level.SEVERE, null, ex);
        }
        //获取上期值时间
        String lastTime = getSQ(repid);
        //获取本期值
        String thistime = getThis(repid);
        //获取时间字段
        String yearcol = getTimeCol(repid);
        //获取维度列表,即上期值所筛选的维度
        //   String demJsonArr = this.getDemlist(facttable, repid, lastTime, yearcol, this.getThis(repid), isshow);
        JSONArray json = new JSONArray();
        String totalCount = null;
        RepDataVO repVo = getRepDemension(repid);
        if (repVo != null) {
            String tablename = repVo.getReffact(); //TF_REP_HRCNT
            String faccol = repVo.getRepvaluecol(); //HTCNT
            String facsumcol = "SUMVAL";
            String dwDimCodeCol = null;
            String dwDimCodeVal = null;
            boolean bDwDim = false;
            ArrayList<RepDemensionVO> repDemLst = repVo.getRepDemLst();
            String timecolid = "";    //时间字段的id
            String timecolname = "";  //时间字段的name
            ArrayList<String> demid = new ArrayList<String>();
            ArrayList<String> demnamecol = new ArrayList<String>();
            ArrayList<String> facdemcol = new ArrayList<String>();
            ArrayList<String> demname = new ArrayList<String>();
            ArrayList<String> demtable = new ArrayList<String>();
            ArrayList<String> demtableNamecol = new ArrayList<String>();
            ArrayList<String> demtableidcol = new ArrayList<String>();
            for (int j = 0; j < repDemLst.size(); j++) {
                RepDemensionVO repDemVO = repDemLst.get(j);
                String dimidcol = repDemVO.getIdcol();
                String facdimcol = repDemVO.getFactdimcol();  //ORG_ID
                String dimnamecol = repDemVO.getNamecol();
                String dimtype = repDemVO.getDemtype();//1单位维度 2时间维度 3其他维度
                String dimname=repDemVO.getDemname();
                demtable.add(repDemVO.getDimtable());
                demtableNamecol.add(repDemVO.getNamecol());
                demtableidcol.add(repDemVO.getIdcol());
                if(dimtype.equals("2")){
                    timecolid=facdimcol;
                    timecolname=dimnamecol;
                }
                demid.add(dimidcol);
                demnamecol.add(dimnamecol);
                facdemcol.add(facdimcol);
                demname.add(dimname);
            }
            String sql="select ";
            for(String str:facdemcol){
                sql+=str+",";
            }
             sql+="repval,sumval,preval,tpreval,tsumval,trepval from "+facttable+" where "+timecolid+"="+lastTime+" and (repval is not null or sumval is not null)";   
            try {
                int count=dao.getDataSetCount(ConstUtil.DBNAME, sql);   //上期填报的数量
                int page=count%1000==0?count/1000:(count/1000+1);       
                for(int j=1;j<=page;j++){
                    Vector v=dao.getDataSet(ConstUtil.DBNAME, sql, j, 1000);
                     if(v!=null){
                    for(int i=0;i<v.size();i++){
                        Hashtable hash=(Hashtable) v.get(i);
                        for(int i2=0;i2<demnamecol.size();i2++){
                            hash.put(facdemcol.get(i2)+demnamecol.get(i2), this.getDimNamebyId(demtable.get(i2), demtableNamecol.get(i2), demtableidcol.get(i2), (String)hash.get(facdemcol.get(i2))));
                            hash.put(timecolid, thistime);
                            hash.put(timecolname, this.getTimeColName(repid, thistime));
                        }
                        v.set(i, hash);
                         json = JSONArray.fromObject(v);
                         String countStr="{listData:"+json.toString()+"}";
                         String returnStr=new FillDataDAO().DatatbUpdate(repid, roleid, countStr);
                         if(!returnStr.equals("ok")){
                             isSuccessed=false;
                         }
                    }
                    String returnStr=new FillDataDAO().DatatbUpdate(repid, roleid, objInfo);
                    if(!returnStr.equals("ok")){
                        isSuccessed=false;
                    }
                }
                }
            } catch (ServletException ex) {
                Logger.getLogger(FillDataSQ.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if(isSuccessed==true){
            res="ok";
        }else{
            res="faile";
        }
        return res;
    }

    /**
     * 判断是否有上期值 返回 no ：没有 返回 ok ：有
     *
     * @param repid
     * @return
     */
    public String hasSQ(String repid) {
        String res = "no";
        DAO dao = new DAO();
        String YearCol = getTimeCol(repid);
        RepDataVO repVo = getRepDemension(repid);
        ArrayList demlist = new ArrayList();
        if (repVo != null) {
            ArrayList<RepDemensionVO> repDemLst = repVo.getRepDemLst();
            for (int j = 0; j < repDemLst.size(); j++) {
                RepDemensionVO repDemVO = repDemLst.get(j);
                String facdimcol = repDemVO.getFactdimcol();
                String dimtype = repDemVO.getDemtype();  //1单位维度 2时间维度 3其他维度
                if (dimtype.equals("2")) {
                    YearCol = facdimcol;
                } else {
                    demlist.add(facdimcol);
                }
            }
            try {
                String facttable_sql = "SELECT REFFACT from tb_report t where repid=" + repid;
                String table = dao.getDataValue(ConstUtil.DBNAME, facttable_sql);
                String sql = "select max(" + YearCol + ") from " + table + "  where (";
                for (int i = 0; i < demlist.size() - 1; i++) {
                    sql += demlist.get(i) + "!= 99999 or ";
                }
                sql += demlist.get(demlist.size() - 1) + "!=99999) and  " + YearCol + "!=99999";
                String result = dao.getDataValue(ConstUtil.DBNAME, sql.toString());
                if (result != null && !result.equals("0")) {
                    res = "ok";
                }
            } catch (ServletException ex) {
                Logger.getLogger(FillDataSQ.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return res;
    }

    /**
     * 获取维度表Name字段
     *
     * @return
     */
    public String getTimeColName(String repid, String thistime) {
        String res = "";
        if (thistime.equals("")) {
            res = "已填报";
            return res;
        }
        DAO dao = new DAO();
        String idcol = "";
        String namecol = "";
        String table = "";
        RepDataVO repVo = getRepDemension(repid);
        if (repVo != null) {
            ArrayList<RepDemensionVO> repDemLst = repVo.getRepDemLst();
            for (int j = 0; j < repDemLst.size(); j++) {
                RepDemensionVO repDemVO = repDemLst.get(j);
                String dimtype = repDemVO.getDemtype();  //1单位维度 2时间维度 3其他维度
                if (dimtype.equals("2")) {
                    idcol = repDemVO.getIdcol();
                    namecol = repDemVO.getNamecol();
                    table = repDemVO.getDimtable();
                }
            }
            String sql = "select " + namecol + " from " + table + " where " + idcol + "=" + thistime;
            try {
                res = dao.getDataValue(ConstUtil.DBNAME, sql);
            } catch (ServletException ex) {
                Logger.getLogger(FillDataSQ.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return res;
    }

    /**
     * 获取上期值，通过筛选事实表里的时间维度，取最大值
     *
     * @param repid
     * @return
     */
    public String getSQ(String repid) {
        String res = "";
        DAO dao = new DAO();
        String YearCol = getTimeCol(repid);
        RepDataVO repVo = getRepDemension(repid);
        ArrayList demlist = new ArrayList();
        if (repVo != null) {
            ArrayList<RepDemensionVO> repDemLst = repVo.getRepDemLst();
            for (int j = 0; j < repDemLst.size(); j++) {
                RepDemensionVO repDemVO = repDemLst.get(j);
                String facdimcol = repDemVO.getFactdimcol();
                String dimtype = repDemVO.getDemtype();  //1单位维度 2时间维度 3其他维度
                if (dimtype.equals("2")) {
                    YearCol = facdimcol;
                } else {
                    demlist.add(facdimcol);
                }
            }
            try {
                String facttable_sql = "SELECT REFFACT from tb_report t where repid=" + repid;
                String table = dao.getDataValue(ConstUtil.DBNAME, facttable_sql);
                String sql = "select max(" + YearCol + ") from " + table;
                String lastime = dao.getDataValue(ConstUtil.DBNAME, sql);
                res = lastime;
            } catch (ServletException ex) {
                Logger.getLogger(FillDataSQ.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return res;
    }

    /**
     * 获取当期值<br/> 新算法：维度的上期值直接用SQL语句筛选，不再判断具体维度 2013.2.5
     *
     * @param repid
     * @return
     */
    public String getThis(String repid) {
        String res = "";
        String lasttime = this.getSQ(repid);
        RepDataVO repVo = getRepDemension(repid);
        String DIMTable = "";
        String IDCOL = "";
        DAO dao = new DAO();
        if (repVo != null) {
            ArrayList<RepDemensionVO> repDemLst = repVo.getRepDemLst();
            for (int j = 0; j < repDemLst.size(); j++) {
                RepDemensionVO repDemVO = repDemLst.get(j);
                String dimtype = repDemVO.getDemtype();  //1单位维度 2时间维度 3其他维度
                if (dimtype.equals("2")) {
                    DIMTable = repDemVO.getDimtable();
                    IDCOL = repDemVO.getIdcol();
                }
            }
            String sql = "select min(" + IDCOL + ") from " + DIMTable + " where " + IDCOL + ">" + lasttime;
            try {
                res = dao.getDataValue(ConstUtil.DBNAME, sql);
                if (res.equals("0")) {      //标记，在MySQL里结果集是null时会返回0，别的数据库有待测试
                    res = "";
                }
            } catch (ServletException ex) {
                Logger.getLogger(FillDataSQ.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return res;
    }
    
    /**判断是否有当期值
     * 
     * @param repid
     * @return 
     */
    public String hasThis(String repid) {
        String res = "ok";
        String lasttime = this.getSQ(repid);
        RepDataVO repVo = getRepDemension(repid);
        String DIMTable = "";
        String IDCOL = "";
        DAO dao = new DAO();
        if (repVo != null) {
            ArrayList<RepDemensionVO> repDemLst = repVo.getRepDemLst();
            for (int j = 0; j < repDemLst.size(); j++) {
                RepDemensionVO repDemVO = repDemLst.get(j);
                String dimtype = repDemVO.getDemtype();  //1单位维度 2时间维度 3其他维度
                if (dimtype.equals("2")) {
                    DIMTable = repDemVO.getDimtable();
                    IDCOL = repDemVO.getIdcol();
                }
            }
            String sql = "select count(1) from " + DIMTable + " where " + IDCOL + ">" + lasttime;
            try {
                res = dao.getDataValue(ConstUtil.DBNAME, sql);
                if (res.equals("0")) {      //标记，在MySQL里结果集是null时会返回0，别的数据库有待测试
                    res = "no";
                }else{
                	res="ok";
                }
            } catch (ServletException ex) {
                Logger.getLogger(FillDataSQ.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return res;
    }

    /**
     * 获取时间字段
     *
     * @return
     */
    public String getTimeCol(String repid) {
        String res = "";
        RepDataVO repVo = getRepDemension(repid);
        if (repVo != null) {
            ArrayList<RepDemensionVO> repDemLst = repVo.getRepDemLst();
            for (int j = 0; j < repDemLst.size(); j++) {
                RepDemensionVO repDemVO = repDemLst.get(j);
                String facdimcol = repDemVO.getFactdimcol();
                String dimtype = repDemVO.getDemtype();  //1单位维度 2时间维度 3其他维度
                if (dimtype.equals("2")) {
                    res = facdimcol;
                }
            }
        }
        return res;
    }

    /**
     * 获取上期值筛选的所有的维度，为本期值的填报提供参照
     *
     * @return
     */
    private String getDemlist(String facttable, String repid, String yearid, String timecol, String thistime, boolean isshow) {
        String res = "";
        DAO dao = new DAO();
        RepDataVO repVo = getRepDemension(repid);
        if (repVo != null) {
            Vector v = new Vector();
            ArrayList<RepDemensionVO> repDemLst = repVo.getRepDemLst();
            for (int j = 0; j < repDemLst.size(); j++) {
                RepDemensionVO repDemVO = repDemLst.get(j);
                String facdimcol = repDemVO.getFactdimcol();
                Hashtable hash = new Hashtable();
                try {
                    //设置维度表表名
                    hash.put("facdimtable", dao.getDataValue(ConstUtil.DBNAME, "SELECT t.DIMTABLE FROM tb_demension t WHERE t.FACDIMCOL='" + facdimcol + "'"));
                    //设置事实表维度字段
                    hash.put("facdimcodecol", dao.getDataValue(ConstUtil.DBNAME, "SELECT t.IDCOL from tb_demension t WHERE t.FACDIMCOL='" + facdimcol + "'"));
                    //设置事实表关联字段的值
                    if (isshow == true) {
                        hash.put("facdimvalue", getDataAll(facttable, facdimcol, yearid, timecol, thistime));
                    } else {
                        hash.put("facdimvalue", getData(facttable, facdimcol, yearid, timecol, thistime));
                    }
                } catch (ServletException ex) {
                    Logger.getLogger(FillDataSQ.class.getName()).log(Level.SEVERE, null, ex);
                }
                v.add(hash);
            }
            JSONArray json = JSONArray.fromObject(v);
            res = "{demlist:" + json.toString() + "}";
        }
        return res;
    }

    /**
     * 如果用户没有勾选参照上期值，则采取此方法，该方法是获取本期值的填报界面
     *
     * @return
     */
    private String getData(String table, String facdimcol, String yearid, String timecol, String thistime) {
        String res = "";
        DAO dao = new DAO();
        String sql = "select distinct " + facdimcol + " from " + table + " where " + timecol + "=" + yearid;
        try {
            Vector v = dao.getDataSet(ConstUtil.DBNAME, sql);
            if (v != null) {
                for (int i = 0; i < v.size(); i++) {
                    Hashtable hash = (Hashtable) v.get(i);
                    Set set = hash.keySet();
                    Object[] str = set.toArray();
                    String getDimcol = "SELECT t.IDCOL from tb_demension t WHERE t.FACDIMCOL='" + str[0] + "'";
                    String id_col = dao.getDataValue(ConstUtil.DBNAME, getDimcol);
                    if (id_col.equals("TIME_ID") || id_col.equals("YEARMONTHWEEK_ID")) {
                        res += thistime + ",";
                    } else {
                        res += hash.get(facdimcol) + ",";
                    }
                }
            } else {
                return "";
            }
        } catch (ServletException ex) {
            Logger.getLogger(FillDataSQ.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res.substring(0, res.length() - 1);
    }

    /**
     * 如果用户勾选了参照上期值，则采用此方法，该方法是获取上期值，生成的是上期值填报界面（上期值则会在传到前台前转化成当期值）
     *
     * @return
     */
    private String getDataAll(String table, String facdimcol, String yearid, String timecol, String thistime) {
        String res = "";
        DAO dao = new DAO();
        String sql = "select distinct " + facdimcol + " from " + table + " where " + timecol + "=" + yearid;
        try {
            Vector v = dao.getDataSet(ConstUtil.DBNAME, sql);
            if (v != null) {
                for (int i = 0; i < v.size(); i++) {
                    Hashtable hash = (Hashtable) v.get(i);
                    res += hash.get(facdimcol) + ",";
                }
            } else {
                return "";
            }
        } catch (ServletException ex) {
            Logger.getLogger(FillDataSQ.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res.substring(0, res.length() - 1);
    }

    /**
     * 加载指标对象
     *
     * @param repid
     * @return
     */
    public RepDataVO getRepDemension(String repid) {
        RepDataVO repVO = new RepDataVO();
        DAO dao = new DAO();
        try {
            String sql = "select * from ( select * from tb_report where repid=" + repid + " and ISVALID=1 ) a"
                    + " left join tb_rep_demension b on b.repid=a.repid"
                    + " left join tb_demension c on c.demid=b.dimid order by b.dimorder";
            Vector v = dao.getDataSet(ConstUtil.DBNAME, sql);
            if (v != null) {
                ArrayList<RepDemensionVO> repDemLst = new ArrayList<RepDemensionVO>();
                for (int i = 0; i < v.size(); i++) {
                    Hashtable hashRows = (Hashtable) v.get(i);
                    RepDemensionVO repDemVO = new RepDemensionVO();
                    repDemVO.setId((String) hashRows.get("ID"));
                    repDemVO.setRepid((String) hashRows.get("REPID"));
                    repDemVO.setDimid((String) hashRows.get("DIMID"));
                    repDemVO.setHirecdesc((String) hashRows.get("HIRECDESC"));
                    repDemVO.setDemname((String) hashRows.get("DEMNAME"));
                    repDemVO.setFactdimcol(((String) hashRows.get("FACDIMCOL")).toUpperCase());
                    repDemVO.setAvailsname((String) hashRows.get("AVAILSNAME"));
                    repDemVO.setDimorder((String) hashRows.get("DIMORDER"));
                    repDemVO.setDemcode((String) hashRows.get("DEMCODE"));
                    repDemVO.setDimtable((String) hashRows.get("DIMTABLE"));
                    repDemVO.setIdcol(((String) hashRows.get("IDCOL")).toUpperCase());
                    repDemVO.setNamecol(((String) hashRows.get("NAMECOL")).toUpperCase());
                    repDemVO.setParentcol(((String) hashRows.get("PARENTCOL")).toUpperCase());
                    repDemVO.setCodecol(((String) hashRows.get("CODECOL")).toUpperCase());
                    repDemVO.setDemtype((String) hashRows.get("DIMTYPE"));
                    repDemVO.setIsvalid((String) hashRows.get("ISVALID"));
                    repDemVO.setLevelcol((String) hashRows.get("LEVELCOL"));
                    repDemLst.add(repDemVO);
                    if (i == v.size() - 1) {
                        repVO.setRepid((String) hashRows.get("REPID"));
                        repVO.setRepcode((String) hashRows.get("REPCODE"));
                        repVO.setRepname((String) hashRows.get("REPNAME"));
                        repVO.setCataid((String) hashRows.get("CATAID"));
                        repVO.setRepdesc((String) hashRows.get("REPDESC"));
                        repVO.setDesc((String) hashRows.get("CALDESC"));
                        repVO.setDataprec((String) hashRows.get("DATAPREC"));
                        repVO.setDataunit((String) hashRows.get("DATAUNIT"));
                        repVO.setDepartment((String) hashRows.get("DEPARTMENT"));
                        repVO.setSourcetype((String) hashRows.get("SOURCETYPE"));
                        repVO.setDatatype((String) hashRows.get("DATATYPE"));
                        repVO.setUpdatetime((String) hashRows.get("UPDATETIME"));
                        repVO.setReffact((String) hashRows.get("REFFACT"));
                        repVO.setRepvaluecol(((String) hashRows.get("REPVALUECOL")).toUpperCase());
                        repVO.setIssum((String) hashRows.get("ISSUM"));
                        repVO.setIsvalid((String) hashRows.get("ISVALID"));
                        repVO.setRepDemLst(repDemLst);
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("FillDataDAO getFillGrid Exception.");
        }
        return repVO;
    }

    public String getDimCodeByID(String dimtablename, String dimidcol, String dimcodecol, String dimidval) {
        String sret = "";
        try {
            DAO dao = new DAO();
            String sql = "select " + dimcodecol + " from " + dimtablename + " where " + dimidcol + "=" + dimidval + "";
            sret = dao.getDataValue(ConstUtil.DBNAMEODS, sql);
        } catch (ServletException ex) {
            Logger.getLogger(CheckLeaf.class.getName()).log(Level.SEVERE, null, ex);
        }
        return sret;
    }
    
    public String getDimNamebyId(String dimtablename, String dimnamecol, String dimidcol, String dimidval){
        String sret = "";
        try {
            DAO dao = new DAO();
            String sql = "select " + dimnamecol + " from " + dimtablename + " where " + dimidcol + "=" + dimidval + "";
            sret = dao.getDataValue(ConstUtil.DBNAMEODS, sql);
        } catch (ServletException ex) {
            Logger.getLogger(CheckLeaf.class.getName()).log(Level.SEVERE, null, ex);
        }
        return sret;
    }
}
